//
//  SWUploadHairProMenueView.m
//  YiYueHairdresser
//
//  Created by Sky on 2018/10/31.
//  Copyright © 2018年 YuYiLian. All rights reserved.
//

#import "SWUploadHairProMenueView.h"

@interface SWUploadHairProMenueView()
@property(nonatomic, strong) UIButton *oldBtn;
@end

@implementation SWUploadHairProMenueView
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initUI];
    }
    return self;
}

- (void)initUI {
    self.backgroundColor = UIColor.whiteColor;
    UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, self.width, 30)];
    titleLab.text = @"作品描述";
    titleLab.font = [UIFont systemFontOfSize:17];
    titleLab.textColor = UIColor.blackColor;
    [self addSubview:titleLab];
    _titleLab = titleLab;
}

-(void)setTitleArray:(NSArray<NSString *> *)titleArray {
    _titleArray = titleArray;
    
    //每个Item宽高
    CGFloat W = 80;
    CGFloat H = 30;
    //每行列数
    NSInteger rank = 3;
    //每列间距
    CGFloat rankMargin = (self.frame.size.width - rank * W) / (rank - 1) - 40;
    //每行间距
    CGFloat rowMargin = 20;
    //Item索引 ->根据需求改变索引
    NSUInteger index = _titleArray.count;
    CGFloat max_y = 0;
    for (int i = 0 ; i< index; i++) {
        //Item X轴
        CGFloat X = (i % rank) * (W + rankMargin);
        //Item Y轴
        NSUInteger Y = (i / rank) * (H +rowMargin);
        //Item top
        CGFloat top = CGRectGetMaxY(_titleLab.frame) + 10;
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:_titleArray[i] forState:UIControlStateNormal];
        [btn setTitleColor:UIColor.grayColor forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        btn.backgroundColor = UIColor.whiteColor;
        btn.layer.masksToBounds = YES;
        btn.layer.cornerRadius = 3;
        btn.layer.borderWidth = 0.5;
        btn.layer.borderColor = UIColor.lightGrayColor.CGColor;
        btn.frame = CGRectMake(X + 20, Y+top, W, H);
        [btn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        btn.tag = i;
        max_y = CGRectGetMaxY(btn.frame);
}
    self.height = max_y + 20;
}

- (void)clickAction:(UIButton *)sender {
    sender.selected = !sender.selected;
   
    if (_oldBtn) {
        [_oldBtn setTitleColor:UIColor.grayColor forState:UIControlStateNormal];
        _oldBtn.backgroundColor = UIColor.whiteColor;
        _oldBtn.layer.borderWidth = 0.5;
    }
    
        [sender setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        sender.backgroundColor = UIColor.orangeColor;
        sender.layer.borderWidth = 0.001;
   
    
    
    _oldBtn = sender;
    self.clickBlock(sender.tag);
}
@end
